package com.youdoneed.user.searchword;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.pagination.Page;
import com.youdoneed.base.pagination.Paging;
import com.youdoneed.user.application.ExcelUtils;
import com.youdoneed.user.model.SearchWord;
import com.youdoneed.user.vo.SearchWordStatistics;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 搜索词请求控制器。
 *
 * @author Ewing
 * @date 2017.2.8
 */
@RestController
@RequestMapping("/searchWord")
@Api(description = "搜索词")
public class SearchWordController {

    @Autowired
    private SearchWordService searchWordService;

    /**
     * 新增搜索词。
     */
    @ApiOperation("新增搜索词")
    @ApiImplicitParams(@ApiImplicitParam(name = "name", value = "名称", required = true, paramType = "query"))
    @RequestMapping(value = "/addSearchWord", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage addSearchWord(SearchWord searchWord) {
        searchWordService.addSearchWord(searchWord);
        return new ResultMessage();
    }

    /**
     * 按条件获取搜索词。
     */
    @ApiOperation("按条件获取搜索词")
    @RequestMapping(value = "/findSearchWords", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Page<SearchWord>> findSearchWords(Paging paging, Date startTime, Date endTime) {
        Page<SearchWord> searchWords = searchWordService.findSearchWords(paging, startTime, endTime);
        return new ResultMessage<>(searchWords);
    }

    /**
     * 管理员导出搜索词并分页。
     */
    @RequestMapping(value = "/exportSearchWords", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation("管理员导出搜索词并分页（必须限制导出条数，默认100，该接口不统计总数）")
    public void exportSearchWords(HttpServletResponse response, Paging paging, Date startTime, Date endTime) {
        paging.setCount(false);
        Page<SearchWord> searchWordPage = searchWordService.findSearchWords(paging, startTime, endTime);
        List<ExportSearchWord> exportSearchWords = searchWordPage.getContent()
                .stream().map(searchWord -> {
                    ExportSearchWord exportSearchWord = new ExportSearchWord();
                    BeanUtils.copyProperties(searchWord, exportSearchWord);
                    return exportSearchWord;
                }).collect(Collectors.toList());
        ExcelUtils.exportToResponse(response, "搜索词导出",
                ExportSearchWord.class, exportSearchWords);
    }

    /**
     * 搜索词统计。
     */
    @ApiOperation("搜索词统计")
    @RequestMapping(value = "/searchWordStatistics", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Page<SearchWordStatistics>> searchWordStatistics(
            Paging paging, Integer minSearchTimes, Integer maxSearchTimes, Date startTime, Date endTime) {
        return new ResultMessage<>(searchWordService
                .searchWordStatistics(paging, minSearchTimes, maxSearchTimes, startTime, endTime));
    }

}
